package org.mspring.mlog.web.module.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.api.rss.NewsRSSGen;
import org.mspring.platform.cache.service.CacheService;
import org.mspring.platform.cache.service.DefaultCacheService;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.mspring.platform.web.render.RSSRender;
import org.mspring.platform.web.render.XMLRender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月26日
 */
@Widget
@RequestMapping("/rss")
public class Web_RSSWidget extends Web_AbstractWidget {
    @Autowired
    private DefaultCacheService defaultCacheService;

    private static final String RSS_NEWS_CACHEKEY = "RSS_NEWS";

    @RequestMapping("/news")
    public void news(HttpServletRequest request, HttpServletResponse response) {
        String rss = "";
        Object obj = defaultCacheService.getDefaultCacheValue(RSS_NEWS_CACHEKEY);
        if (obj != null && StringUtils.isNotBlank(obj.toString())) {
            rss = obj.toString();
        }
        if (StringUtils.isBlank(rss)) {
            NewsRSSGen gen = new NewsRSSGen();
            rss = gen.gen();
        }
        defaultCacheService.updateDefaultCacheValue(RSS_NEWS_CACHEKEY, rss, CacheService.ONE_HOUR);
/*        XMLRender render = new XMLRender();
        render.setContent(rss);
        render.render(response);
 * */
        RSSRender render = new RSSRender(rss);
        render.render(response);
    }
}
